//
//  ViewController.m
//  AVAssetImageGenerator
//
//  Created by Xwoder on 2017/2/6.
//  Copyright © 2017年 Xwoder. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "UIView+ALQuickFrame.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"movie" withExtension:@"mp4"];
    
    AVAsset *asset = [AVAsset assetWithURL:url];
    
    // AVAssetImageGenerator
    AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
    imageGenerator.maximumSize = CGSizeZero;
    imageGenerator.appliesPreferredTrackTransform = NO;
    CMTime time = CMTimeMake(2, 1);
    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
    
    UIImage *image = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    
    CGFloat imageWidth = image.size.width;
    CGFloat imageHeight = image.size.height;
    CGFloat scale = imageWidth / imageHeight;
    CGSize imageViewSize = CGSizeMake(self.view.width, self.view.width / scale);
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.size = imageViewSize;
    imageView.center = self.view.center;
    [self.view addSubview:imageView];
}

@end
